// pages/ag/index.js
var app = getApp(), fetchApi = require("../../utils/httpPost.js"), common_js = require("../../component/common.js")
import Toast from '../../component/vant/toast/toast';
import Dialog from '../../component/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ag_id: 0,
    phone_num: '',
    face_url: '',
    nick_name: '',
    promotion_qr_code:'',
    user_no:'',
    is_has_ag: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.ag_id){
      this.data.ag_id = options.ag_id;
    }
    this.onHttpInit();
  },

  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/user/promotion/detail', {
      ag_id: that.data.ag_id,
    }, function (res) {
      console.log("获取代理的详细信息", res)
      if (res.data.code == 0) {
        that.data.is_has_ag = res.data.data.is_has_ag;
        that.setData({
          is_has_ag: res.data.data.is_has_ag,
          phone_num: res.data.data.phone_num,
          face_url: res.data.data.face_url,
          nick_name: res.data.data.nick_name,
          promotion_qr_code: res.data.data.promotion_qr_code,
          user_no: res.data.data.user_no,
        })
      }
      that.setData({ netWork: true, isLoading: false, loading: true})
    },function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, loading: false })
    })
  },
  goBand(){
    var that = this;
    if(this.data.is_has_ag == 0){
      var message = "是否要绑定改推广代理？";
      Dialog.confirm({
          title: '绑定推广代理',
          message
        }).then(() => {
          console.log('123123123')
          that.onHttpBand();
        })
        .catch(() => {
          Dialog.close();
        });
    }else{
      Toast('您已绑定推广代理')
    }
  },  
  onHttpBand(){
    console.log('onHttpBand', 123123123)
    var that = this;
    fetchApi.httpPost('/user/bandPromoter', {
      promoter_ag_id: that.data.ag_id,
    }, function (res) {
      console.log("绑定代理", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1
          })
        }, 300);
      }else{
        Toast.fail(res.data.msg);
      }
      that.setData({ netWork: true, isLoading: false, loading: true})
    },function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, loading: false })
    })
  },
})